﻿
-----------------------------------------------------------------------
-- MatrixHook.lua
--
-- A Framework for World Of Warcraft addons
-- Copyright (C) 2007  wwkkii
-- 
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
-- 
-- Home Page: http://code.google.com/p/wowmatrix
-----------------------------------------------------------------------

local __version__ = { "MatrixHook", 0, 1, 1 };
if (MatrixUpdateVersion(__version__)) then return; end

-----------------------------------------------------------------------
-- MatrixHooker Class
-----------------------------------------------------------------------

MatrixClass("MatrixHooker", MatrixObject, MATRIX_NORMAL_CLASS);

function MatrixHooker:__call(...)
	return self.raw(...);
end

function MatrixHooker:Initial(owner, name, proc)
	self:Super(name);
	self.owner = owner;
	if (owner == nil) then
		self.raw = getglobal(name);
		setglobal(name, proc);
	else
		self.raw = owner[name];
		rawset(owner, name, proc);
	end
end

function MatrixHooker:Final()
	if (self.owner == nil) then
		setglobal(self:Name(), self.raw);
	else
		rawset(self.owner, self:Name(), self.raw);
	end
	self.owner = nil;
	self.raw = nil;
end

-----------------------------------------------------------------------
-- Use MatrixHooker:
-- local hookerOfTester;
-- local function NewFunctionOfTester(...)
-- 	/* do hooker action */
-- 	return hookerOfTester(...);
-- end
--
-- hookerOfTester = MatrixHooker:New(nil, "ChatFrame_OnEvent", NewFunctionOfTester);
-----------------------------------------------------------------------
